<% session.setAttribute("title", "Approve Proposal"); %> 
<%@ include file="Header.jsp"%>

<%
	org.jbpm.api.ProcessEngine processEngine = StaticProcessEngine.getInstance();
	TaskService taskService = processEngine.getTaskService();
	String taskId = request.getParameter("task_id");

	if (request.getParameter("approve") != null) {
		Set<String> variableNames = taskService.getVariableNames(taskId);
		Map<String, Object> variables = taskService.getVariables(taskId, variableNames);
		variables.put("approval_description", request.getParameter("approval_description"));
		variables.put("reviewer", request.getParameter("reviewer"));
		taskService.completeTask(taskId, "to ApproveProposalTask",variables);
	} else if (request.getParameter("reject") != null) {
		Set<String> variableNames = taskService.getVariableNames(taskId);
		Map<String, Object> variables = taskService.getVariables(taskId, variableNames);
		variables.put("approval_description", request.getParameter("approval_description"));
		taskService.completeTask(taskId, "to RejectProposalTask", variables);
	} 

	List<Task> tasks = taskService.findPersonalTasks(session.getAttribute("username").toString());

	if (tasks.isEmpty()) { %>
		No pending proposals for you
	<% }

	for (Task task : tasks) {
		taskId = task.getId();
		Set<String> variableNames = taskService.getVariableNames(taskId);
		Map<String, Object> variables = taskService.getVariables(taskId, variableNames);
	%>
		<br />
		Proposal title:
		<%=variables.get("title")%><br />
		<form action="ProposalApprovalPage.jsp">
		<table cellpadding="5" cellspacing="5">
			<tr>
				<td>Reviewer:</td>
				<td><input type="text" name="reviewer" /></td>
			</tr>
			<tr>
				<td>Description:</td>
				<td> <input type="text" name="approval_description" /></td>
			</tr>
			
			<tr>
				<td><input type="submit" name="approve" value="Approve" /></td>
				<td><input	type="submit" name="reject" value="Reject" /></td>
			</tr> 
			<input type="hidden" name="proposal_id" value="<%=variables.get("proposal_id")%>" /> 
			<input type="hidden"name="task_id" value="<%=taskId%>" /> 
		</table>
		</form>
	<% } %> 
	<br />
	<br />
<%@ include file="Footer.jsp" %>